<?php
session_start();
if (!isset($_SESSION['admin'])) { header('Location: index.php'); exit; }
$pid = $_GET['pid'] ?? '';
$projects = file_exists('../data/projects.json') ? json_decode(file_get_contents('../data/projects.json'), true) : [];
if (!$pid || !isset($projects[$pid])) { echo '项目不存在'; exit; }
$dir = "../data/project_$pid/";
$dataFile = $dir.'app_info.json';
$iconDir = $dir.'icon/';
$iconFile = $iconDir.'icon.png';
if (!is_dir($iconDir)) mkdir($iconDir, 0777, true);

// 获取项目语言设置
$projectLanguage = $projects[$pid]['language'] ?? 'zh';

// 处理保存和ICON上传
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $info = [
        'name' => $_POST['name'],
        'desc' => $_POST['desc'],
        'rating' => $_POST['rating'],
        'developer' => $_POST['developer']
    ];
    
    // 如果是英文项目，添加英文内容
    if ($projectLanguage === 'en') {
        $info['name_en'] = $_POST['name_en'] ?? $_POST['name'];
        $info['desc_en'] = $_POST['desc_en'] ?? $_POST['desc'];
        $info['developer_en'] = $_POST['developer_en'] ?? $_POST['developer'];
    }
    
    if (isset($_FILES['icon']) && $_FILES['icon']['error'] === 0) {
        $ext = strtolower(pathinfo($_FILES['icon']['name'], PATHINFO_EXTENSION));
        if (in_array($ext, ['png','jpg','jpeg','svg'])) {
            move_uploaded_file($_FILES['icon']['tmp_name'], $iconFile);
            $info['icon'] = "data/project_$pid/icon/icon.png";
        }
    } elseif (file_exists($iconFile)) {
        $info['icon'] = "data/project_$pid/icon/icon.png";
    }
    file_put_contents($dataFile, json_encode($info, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
    header('Location: project_dashboard.php?pid='.$pid); exit;
}
// ICON删除
if (isset($_GET['delicon'])) {
    if (file_exists($iconFile)) unlink($iconFile);
    $info = file_exists($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
    unset($info['icon']);
    file_put_contents($dataFile, json_encode($info, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
    header('Location: app_info.php?pid='.$pid); exit;
}
$info = file_exists($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
$iconUrl = isset($info['icon']) && file_exists("../".$info['icon']) ? '../'.$info['icon'] : '';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>应用信息管理</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
    .icon-preview { width:72px; height:72px; border-radius:16px; object-fit:cover; border:1px solid #eee; background:#fafbfc; }
    </style>
</head>
<body>
<div class="container py-4">
    <h4 class="mb-3">应用信息管理 - <?=htmlspecialchars($projects[$pid]['name'])?></h4>
    <?php if(isset($msg)) echo '<div class="alert alert-success">'.$msg.'</div>'; ?>
    <form method="post" enctype="multipart/form-data" class="bg-white p-4 rounded shadow-sm">
        <div class="mb-3">
            <label class="form-label">APP ICON（建议512x512，PNG/JPG/SVG）</label><br>
            <?php if($iconUrl): ?>
                <img src="<?=$iconUrl?>" class="icon-preview mb-2">
                <a href="?pid=<?=$pid?>&delicon=1" class="btn btn-sm btn-outline-danger ms-2" onclick="return confirm('确定删除ICON？')">删除ICON</a>
            <?php else: ?>
                <span class="text-muted">暂无ICON</span>
            <?php endif; ?>
            <input type="file" name="icon" accept=".png,.jpg,.jpeg,.svg" class="form-control mt-2" style="max-width:300px;">
        </div>
        <div class="mb-3">
            <label class="form-label">应用名称</label>
            <input name="name" class="form-control" value="<?=htmlspecialchars($info['name']??'')?>" required>
        </div>
        <?php if ($projectLanguage === 'en'): ?>
        <div class="mb-3">
            <label class="form-label">应用名称 (English)</label>
            <input name="name_en" class="form-control" value="<?=htmlspecialchars($info['name_en']??$info['name']??'')?>" required>
        </div>
        <?php endif; ?>
        <div class="mb-3">
            <label class="form-label">简介</label>
            <textarea name="desc" class="form-control" rows="3" required><?=htmlspecialchars($info['desc']??'')?></textarea>
        </div>
        <?php if ($projectLanguage === 'en'): ?>
        <div class="mb-3">
            <label class="form-label">简介 (English)</label>
            <textarea name="desc_en" class="form-control" rows="3" required><?=htmlspecialchars($info['desc_en']??$info['desc']??'')?></textarea>
        </div>
        <?php endif; ?>
        <div class="mb-3">
            <label class="form-label">评分</label>
            <input name="rating" class="form-control" value="<?=htmlspecialchars($info['rating']??'')?>">
        </div>
        <div class="mb-3">
            <label class="form-label">开发者</label>
            <input name="developer" class="form-control" value="<?=htmlspecialchars($info['developer']??'')?>">
        </div>
        <?php if ($projectLanguage === 'en'): ?>
        <div class="mb-3">
            <label class="form-label">开发者 (English)</label>
            <input name="developer_en" class="form-control" value="<?=htmlspecialchars($info['developer_en']??$info['developer']??'')?>">
        </div>
        <?php endif; ?>
        <div class="d-flex justify-content-end gap-3 mt-4">
            <button type="submit" class="btn btn-primary btn-lg px-5">保存</button>
            <a href="project_dashboard.php?pid=<?=$pid?>" class="btn btn-outline-secondary btn-lg px-5">返回</a>
        </div>
    </form>
</div>
</body>
</html> 